While it is not inherently wrong to use the closing PHP tag, developers often choose to omit it to minimize the chances of encountering issues and
to follow established best practices and coding standards.
What is the potential impact?
There are a few reasons developers should avoid using the closing PHP tag ?>
at the end of a file:
If a developer accidentally adds whitespace or any other characters after the closing PHP tag, it can cause PHP to automatically start output
buffering. This can lead to unexpected behavior when trying to modify headers or perform redirects because headers need to be sent before any
content.
Some frameworks and coding standards explicitly discourage the use of the closing PHP tag to ensure compatibility and portability across different
PHP environments and versions.
Following a consistent coding style and adhering to best practices are important for code readability and maintainability. Many coding standards,
such as the PER (PHP Evolving Recommendation), recommend omitting the closing PHP tag for the reasons mentioned above.